import java.awt.Graphics2D;
import java.awt.geom.Path2D;
import java.awt.geom.Point2D;
import java.io.Serializable;

public class ArrowHead implements Paintable, Serializable {
	private Configuration config;
	private Path2D head;
	private Arrow arrow;
	
	public ArrowHead(Configuration config, Arrow arrow) {
		this.config = config;
		this.arrow = arrow;
	}

    /**
     * Updates the arrowheads position and orientation so the tip of the arrowhead is at the to end of the arrow and
     * the orientation of the arrowhead matches that of the arrow.
     */
	public void update() {
		Point2D tip = arrow.getToPoint();
		double angle = arrow.getToAngle();
		double angle1 = Math.PI + angle - config.arrowHeadAngle;
		double angle2 = Math.PI + angle + config.arrowHeadAngle;
		Point2D p1 = new Point2D.Double(tip.getX() + config.arrowHeadLength
				* Math.cos(angle1), tip.getY() + config.arrowHeadLength
				* Math.sin(angle1));
		Point2D p2 = new Point2D.Double(tip.getX() + config.arrowHeadLength
				* Math.cos(angle2), tip.getY() + config.arrowHeadLength
				* Math.sin(angle2));
		
		head = new Path2D.Double();
		head.moveTo(tip.getX(), tip.getY());
		head.lineTo(p1.getX(), p1.getY());
		head.lineTo(p2.getX(), p2.getY());
		head.closePath();
	}

	@Override
	public void paint(Graphics2D g) {
		g.fill(head);
	}
}
